
package net.colah.k1.application.impl.core;

import java.util.List;
import java.util.ArrayList;
import java.text.MessageFormat;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.Page;
import org.dayatang.querychannel.QueryChannelService;
import net.colah.k1.application.dto.*;
import net.colah.k1.application.core.SupplierApplication;
import net.colah.k1.core.*;

@Named
@Transactional
public class SupplierApplicationImpl implements SupplierApplication {


	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public SupplierDTO getSupplier(Long id) {
		Supplier supplier = Supplier.load(Supplier.class, id);
		SupplierDTO supplierDTO = new SupplierDTO();
		// 将domain转成VO
		try {
			BeanUtils.copyProperties(supplierDTO, supplier);
		} catch (Exception e) {
			e.printStackTrace();
		}
		supplierDTO.setId((java.lang.Long)supplier.getId());
		return supplierDTO;
	}
	
	public SupplierDTO saveSupplier(SupplierDTO supplierDTO) {
		Supplier supplier = new Supplier();
		try {
        	BeanUtils.copyProperties(supplier, supplierDTO);
        } catch (Exception e) {
        	e.printStackTrace();
        }
		supplier.save();
		supplierDTO.setId((java.lang.Long)supplier.getId());
		return supplierDTO;
	}
	
	public void updateSupplier(SupplierDTO supplierDTO) {
		Supplier supplier = Supplier.get(Supplier.class, supplierDTO.getId());
		// 设置要更新的值
		try {
			BeanUtils.copyProperties(supplier, supplierDTO);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void removeSupplier(Long id) {
		this.removeSuppliers(new Long[] { id });
	}
	
	public void removeSuppliers(Long[] ids) {
		for (int i = 0; i < ids.length; i++) {
			Supplier supplier = Supplier.load(Supplier.class, ids[i]);
			supplier.remove();
		}
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<SupplierDTO> findAllSupplier() {
		List<SupplierDTO> list = new ArrayList<SupplierDTO>();
		List<Supplier> all = Supplier.findAll(Supplier.class);
		for (Supplier supplier : all) {
			SupplierDTO supplierDTO = new SupplierDTO();
			// 将domain转成VO
			try {
				BeanUtils.copyProperties(supplierDTO, supplier);
			} catch (Exception e) {
				e.printStackTrace();
			}
			list.add(supplierDTO);
		}
		return list;
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Page<SupplierDTO> pageQuerySupplier(SupplierDTO queryVo, int currentPage, int pageSize) {
		List<SupplierDTO> result = new ArrayList<SupplierDTO>();
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _supplier from Supplier _supplier   where 1=1 ");
	
	
	   	if (queryVo.getName() != null && !"".equals(queryVo.getName())) {
	   		jpql.append(" and _supplier.name like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getName()));
	   	}		
	
	   	if (queryVo.getCode() != null && !"".equals(queryVo.getCode())) {
	   		jpql.append(" and _supplier.code like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getCode()));
	   	}		
	
	   	if (queryVo.getTel() != null && !"".equals(queryVo.getTel())) {
	   		jpql.append(" and _supplier.tel like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getTel()));
	   	}		
	
	   	if (queryVo.getMobile() != null && !"".equals(queryVo.getMobile())) {
	   		jpql.append(" and _supplier.mobile like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getMobile()));
	   	}		
	
	   	if (queryVo.getFax() != null && !"".equals(queryVo.getFax())) {
	   		jpql.append(" and _supplier.fax like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getFax()));
	   	}		
	
	   	if (queryVo.getAddress() != null && !"".equals(queryVo.getAddress())) {
	   		jpql.append(" and _supplier.address like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getAddress()));
	   	}		
	
	   	if (queryVo.getEmail() != null && !"".equals(queryVo.getEmail())) {
	   		jpql.append(" and _supplier.email like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getEmail()));
	   	}		
	
	   	if (queryVo.getSite() != null && !"".equals(queryVo.getSite())) {
	   		jpql.append(" and _supplier.site like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getSite()));
	   	}		
	
	   	if (queryVo.getContact() != null && !"".equals(queryVo.getContact())) {
	   		jpql.append(" and _supplier.contact like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getContact()));
	   	}		
	
	   	if (queryVo.getMeno() != null && !"".equals(queryVo.getMeno())) {
	   		jpql.append(" and _supplier.meno like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getMeno()));
	   	}		
        Page<Supplier> pages = getQueryChannelService().createJpqlQuery(jpql.toString()).setParameters(conditionVals).setPage(currentPage, pageSize).pagedList();
        for (Supplier supplier : pages.getData()) {
            SupplierDTO supplierDTO = new SupplierDTO();
            
             // 将domain转成VO
            try {
            	BeanUtils.copyProperties(supplierDTO, supplier);
            } catch (Exception e) {
            	e.printStackTrace();
            } 
            
                                                                              result.add(supplierDTO);
        }
        return new Page<SupplierDTO>(pages.getPageIndex(), pages.getResultCount(), pageSize, result);
	}
	
	
}
